import random
class Role(object):
    def __init__(self, name, sex, blood=100):
        self.name = name
        self.sex = sex
        self.blood = blood
        self.room = None
    def attach(self, other):
        fight = {10: "屠龙刀", 20: "倚天剑"}
        if self == other:
            print("不能攻击自己")
        else:
            if self.room and self.room == other.room:
                 if self.blood <= 0:
                    print("您的血量低于0，不能进行攻击")
                 else:
                    if random.choice([0, 1]) == 0:
                        lost_blood = random.choice([10, 20])
                        print(f"{other.name}没有使用护盾")
                    else:
                        lost_blood = random.choice([10, 20])
                        print(f"{other.name}使用了护盾")
                    other.blood = other.blood - lost_blood
                    print(f"{self.name}使用{fight[lost_blood]}攻击了{other.name}{lost_blood}点血量，当前血量{other.blood}")
                    if other.blood <= 0:
                        print(f"{other.name}已阵亡，不能再进行攻击")
                        exit()
            else:
                print("不在同一个房间不能攻击!")



class Room(object):
    def __init__(self, name):
        self.name = name
        self.member_list = []
    def add_member(self, role):
        if role.room == None:
            if len(self.member_list) < 2:
                self.member_list.append(role)
                role.room = self
                print(f"{role.name}成功加入{self.name}房间")
        else:
            print(f"{role.name}已加入{role.room.name}房间")


def ui():
    print('''################
    1.创建角色
    2.创建房间
    3.加入房间
    4.开始战斗
    5.退出
################''')


def main():
    roles = {}
    rooms = {}
    while True:
        ui()
        option = input("请输入您的操作：\n")
        if option.isdigit():
            option = int(option)
            if 1 <= option <= 5:
                if option == 1:
                    name,sex = input("请输入角色的姓名与性别，空格分隔：\n").split()
                    roles[name] = Role(name,sex)
                elif option == 2:
                    room = input("请输入房间的名字：\n")
                    rooms[room] = Room(room)
                elif option == 3:
                    print(f"当前角色有{roles.keys()}，当前房间有{rooms.keys()}")
                    role_name, room_name = input("请输入您要加入的角色和房间名，空格分隔:\n").split()
                    rooms[room_name].add_member(roles[role_name])
                elif option == 4:
                    while True:
                        rooms[room_name].member_list[0].attach(rooms[room_name].member_list[1])
                        rooms[room_name].member_list[1].attach(rooms[room_name].member_list[0])
            else:
                print("输入错误！请输入1-5选择操作！")
        else:
            print("输入有误！请重新输入！")

main()